package com.net.xpay.core.manager.impl;

import com.google.common.base.Preconditions;
import com.net.xpay.core.manager.AccountDetailManager;
import com.net.common.annotation.ReadTransactional;
import com.net.common.annotation.WriteTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.manager.AbstractVersionedManager;
import com.net.noah.core.service.SerialNumberService;
import com.net.xpay.core.dao.AccountDetailMapper;
import com.net.xpay.core.domain.AccountDetail;
import com.net.xpay.core.enums.AccountBillType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;


@Component
@ReadTransactional
public class AccountDetailManagerImpl extends AbstractVersionedManager<AccountDetail> implements AccountDetailManager {

    @Autowired
    private AccountDetailMapper mapper;

    @Autowired
    private SerialNumberService serialNumberService;

    @Override
    @WriteTransactional(propagation = Propagation.SUPPORTS)
    public int save(AccountDetail obj) {
        Preconditions.checkArgument(obj.getId() == null);
        obj.setId(serialNumberService.getLongSerialNumber(getTableFullName()));
        return super.save(obj);
    }

    @Override
    protected BasicMapper<Long, AccountDetail> getMapper() {
        return mapper;
    }

    @Override
    public AccountDetail findByRelatedId(Long userId, Long relatedId, AccountBillType type) {
        Preconditions.checkArgument(userId != null && relatedId != null && type != null);
        AccountDetail condition = new AccountDetail();
        condition.setUserId(userId);
        condition.setRelatedId(relatedId);
        condition.setType(type);
        return getByCondition(condition);
    }
}
